<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/%E6%AD%A6%E6%B1%8932x32.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/%E6%AD%A6%E6%B1%8916x16.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib1/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="/lib1/pace/pace-theme-center-simple.min.css">
  <script src="/lib1/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"example.com","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="前言hooks的概念之类的就不赘述了，之前用react中的class来实现了一个todoList的效果，这里同样用hooks来实现一个这个效果的复现，通过实际行动来实现熟悉hooks的目的。同时，这里我们使用redux来实现react hooks中的数据状态控制。 todoListreducer在reducer中定义该模块中能对组件中数据进行的各类操作，先定义好action.type，之后直接使用">
<meta property="og:type" content="article">
<meta property="og:title" content="react hooks初学">
<meta property="og:url" content="http://example.com/2021/05/14/react/react%20hooks/index.html">
<meta property="og:site_name" content="Technological Blog">
<meta property="og:description" content="前言hooks的概念之类的就不赘述了，之前用react中的class来实现了一个todoList的效果，这里同样用hooks来实现一个这个效果的复现，通过实际行动来实现熟悉hooks的目的。同时，这里我们使用redux来实现react hooks中的数据状态控制。 todoListreducer在reducer中定义该模块中能对组件中数据进行的各类操作，先定义好action.type，之后直接使用">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-05-14T04:18:39.000Z">
<meta property="article:modified_time" content="2021-11-03T08:20:52.386Z">
<meta property="article:author" content="Li Yudong">
<meta property="article:tag" content="react初学">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://example.com/2021/05/14/react/react%20hooks/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>react hooks初学 | Technological Blog</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">Technological Blog</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">IT小白的成长之旅</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="tags fa-fw"></i>标签<span class="badge">18</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="th fa-fw"></i>分类<span class="badge">14</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="archive fa-fw"></i>归档<span class="badge">95</span></a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

  <a href="https://github.com/tiarmor1" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/05/14/react/react%20hooks/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/author.jpg">
      <meta itemprop="name" content="Li Yudong">
      <meta itemprop="description" content="">
    </span>
    
    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Technological Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          react hooks初学
        </h1>
    
        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
    
              <time title="创建时间：2021-05-14 12:18:39" itemprop="dateCreated datePublished" datetime="2021-05-14T12:18:39+08:00">2021-05-14</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-11-03 16:20:52" itemprop="dateModified" datetime="2021-11-03T16:20:52+08:00">2021-11-03</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/react/" itemprop="url" rel="index"><span itemprop="name">react</span></a>
                </span>
            </span>
    
          
    
        </div>
      </header>
    
    
    
    
    <div class="post-body" itemprop="articleBody">
    
      
        <h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>hooks的概念之类的就不赘述了，之前用react中的class来实现了一个todoList的效果，这里同样用hooks来实现一个这个效果的复现，通过实际行动来实现熟悉hooks的目的。同时，这里我们使用redux来实现react hooks中的数据状态控制。</p>
<h1 id="todoList"><a href="#todoList" class="headerlink" title="todoList"></a>todoList</h1><h2 id="reducer"><a href="#reducer" class="headerlink" title="reducer"></a>reducer</h2><p>在reducer中定义该模块中能对组件中数据进行的各类操作，先定义好action.type，之后直接使用即可。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123;createStore&#125; <span class="keyword">from</span> <span class="string">"redux"</span>;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">reducer</span>(<span class="params">state=[],action</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">switch</span>(action.type)&#123;</span><br><span class="line">        <span class="keyword">case</span> <span class="string">"ADD"</span>:</span><br><span class="line">            <span class="comment">//console.log(action.val);</span></span><br><span class="line">            state.push(&#123;</span><br><span class="line">                id: <span class="built_in">Date</span>.now(),</span><br><span class="line">                txt: action.val,</span><br><span class="line">                done: <span class="literal">false</span></span><br><span class="line">            &#125;)</span><br><span class="line">            <span class="comment">//全局的数据部署以简洁为主，只和某个组件有关的数据分别在组件中定义即可</span></span><br><span class="line">            <span class="keyword">return</span> [...state];</span><br><span class="line">        <span class="keyword">case</span> <span class="string">"DONE"</span>:</span><br><span class="line">            state.forEach(<span class="function"><span class="params">item</span>=&gt;</span>&#123;</span><br><span class="line">                <span class="keyword">if</span>(item.id === action.id)&#123;</span><br><span class="line">                    item.done = action.done      </span><br><span class="line">                &#125;</span><br><span class="line">            &#125;) </span><br><span class="line">            <span class="keyword">return</span> [...state];</span><br><span class="line">        <span class="keyword">case</span> <span class="string">"REMOVE"</span>:</span><br><span class="line">            <span class="keyword">return</span> state.filter(<span class="function"><span class="params">item</span>=&gt;</span>(item.id !== action.id));</span><br><span class="line">        <span class="keyword">case</span> <span class="string">"EDIT"</span>:</span><br><span class="line">            state.forEach(<span class="function"><span class="params">item</span>=&gt;</span>&#123;</span><br><span class="line">                <span class="keyword">if</span>(item.id === action.id)&#123;</span><br><span class="line">                    item.txt = action.txt      </span><br><span class="line">                &#125;</span><br><span class="line">            &#125;)  </span><br><span class="line">            <span class="keyword">return</span>  [...state];   </span><br><span class="line">        <span class="keyword">case</span> <span class="string">"REMOVE_DONE"</span>:</span><br><span class="line">            <span class="keyword">return</span> state.filter(<span class="function"><span class="params">item</span>=&gt;</span>!item.done);          </span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> state;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> store = createStore(reducer);</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> store;</span><br></pre></td></tr></table></figure>

<h2 id="App-js"><a href="#App-js" class="headerlink" title="App.js"></a>App.js</h2><p>对整个项目各个模块进行简单的划分，</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">"react"</span>;</span><br><span class="line"><span class="keyword">import</span> <span class="string">"./static/index.css"</span>;</span><br><span class="line"><span class="keyword">import</span> Title <span class="keyword">from</span> <span class="string">"./view/title"</span>;</span><br><span class="line"><span class="keyword">import</span> Todos <span class="keyword">from</span> <span class="string">"./view/todos"</span>;</span><br><span class="line"><span class="keyword">import</span> Create <span class="keyword">from</span> <span class="string">"./view/create"</span>;</span><br><span class="line"><span class="keyword">import</span> State <span class="keyword">from</span> <span class="string">"./view/state"</span>;</span><br><span class="line"><span class="keyword">import</span> &#123;connect&#125; <span class="keyword">from</span> <span class="string">"react-redux"</span>;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">App</span>(<span class="params">props</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">let</span> &#123;length&#125; = props;</span><br><span class="line">    <span class="keyword">return</span> (<span class="xml"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"todoapp"</span>&gt;</span></span></span><br><span class="line"><span class="xml">        <span class="tag">&lt;<span class="name">Title</span>/&gt;</span></span></span><br><span class="line">        &lt;div className="content"&gt;</span><br><span class="line">            &lt;Create/&gt;</span><br><span class="line">            &#123;length&gt;0?&lt;Todos/&gt;:""&#125;</span><br><span class="line">            &#123;length&gt;0?&lt;State/&gt;:""&#125;</span><br><span class="line"><span class="xml">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> connect(<span class="function"><span class="params">state</span> =&gt;</span> (&#123;</span><br><span class="line">    length:state.length</span><br><span class="line">    <span class="comment">//箭头函数要返回一个对象，用括号将对象包裹起来</span></span><br><span class="line">&#125;))(App);</span><br></pre></td></tr></table></figure>

<h2 id="Create-js"><a href="#Create-js" class="headerlink" title="Create.js"></a>Create.js</h2><p>该组件实现用户在输入框中添加待办事项的功能。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">"react"</span>;</span><br><span class="line"><span class="keyword">import</span> &#123;connect&#125; <span class="keyword">from</span> <span class="string">"react-redux"</span>;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Create</span>(<span class="params">props</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">let</span> &#123;dispatch&#125; = props;</span><br><span class="line">    <span class="keyword">return</span> (<span class="xml"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"create-todo"</span>&gt;</span></span></span><br><span class="line">        &lt;input</span><br><span class="line">            id="new-todo"</span><br><span class="line">            placeholder="你需要做什么"</span><br><span class="line">            autoComplete="off"</span><br><span class="line">            type="text"</span><br><span class="line">            defaultValue=''</span><br><span class="line">            onKeyDown=&#123;(e) =&gt; &#123;</span><br><span class="line">                if (e.keyCode == 13) &#123;</span><br><span class="line">                    dispatch(&#123;</span><br><span class="line">                        type: "ADD",</span><br><span class="line">                        val: e.target.value</span><br><span class="line">                    &#125;);</span><br><span class="line">                    e.target.value = "";</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;&#125;</span><br><span class="line"><span class="xml">        /&gt;</span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span>)</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> connect()(Create);</span><br></pre></td></tr></table></figure>

<h2 id="Todos-js"><a href="#Todos-js" class="headerlink" title="Todos.js"></a>Todos.js</h2><p>在这里，我们接受app中传递过来的数据，同时对于每一项todo都以每一列来单独进行处理，这里我们将其封装在li.js中，这样就只用在todos.js中将内容分别分发到li.js中即可。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">"react"</span>;</span><br><span class="line"><span class="keyword">import</span> &#123;connect&#125; <span class="keyword">from</span> <span class="string">"react-redux"</span>;</span><br><span class="line"><span class="keyword">import</span> Li <span class="keyword">from</span> <span class="string">'./li'</span>;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Todos</span>(<span class="params">props</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">let</span> &#123;state, dispatch&#125; = props;</span><br><span class="line">    <span class="keyword">return</span> (<span class="xml"><span class="tag">&lt;<span class="name">ul</span> <span class="attr">id</span>=<span class="string">"todo-list"</span>&gt;</span></span></span><br><span class="line"><span class="xml">        &#123;state.map(item =&gt; <span class="tag">&lt;<span class="name">Li</span> <span class="attr">data</span>=<span class="string">&#123;item&#125;</span> <span class="attr">key</span>=<span class="string">&#123;item.id&#125;</span> <span class="attr">dispatch</span>=<span class="string">&#123;dispatch&#125;/</span>&gt;</span>)&#125;</span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span>)</span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> connect(<span class="function">(<span class="params">state</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> &#123;state&#125;</span><br><span class="line">&#125;)(Todos);</span><br></pre></td></tr></table></figure>

<h2 id="li-js"><a href="#li-js" class="headerlink" title="li.js"></a>li.js</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> React, &#123;useEffect, useState, useRef&#125; <span class="keyword">from</span> <span class="string">"react"</span>;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Li</span>(<span class="params">props</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">let</span> &#123;data, dispatch&#125; = props;</span><br><span class="line">    <span class="keyword">let</span> &#123;id, txt, done&#125; = data;</span><br><span class="line">    <span class="keyword">const</span> [edit, setEdit] = useState(<span class="literal">false</span>);<span class="comment">//在edit中保存的数据只需要一个布尔值即可</span></span><br><span class="line">    <span class="keyword">const</span> [inputVal, setInputVal] = useState(txt);</span><br><span class="line">    <span class="comment">//这样设置，txt做了修改之后，inputVal会自动和txt进行同步.</span></span><br><span class="line">    <span class="keyword">const</span> input = useRef();</span><br><span class="line">    useEffect(<span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (edit) &#123;</span><br><span class="line">            input.current.focus();<span class="comment">//当你要编辑时，则对当前元素进行聚焦</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;, [edit]);</span><br><span class="line">    <span class="keyword">return</span> (</span><br><span class="line">        &lt;li className=&#123;edit?<span class="string">"editing"</span>:<span class="string">""</span>&#125;&gt;</span><br><span class="line">            &lt;div className=&#123;<span class="string">"todo "</span> + (done? <span class="string">"done"</span>:<span class="string">""</span>)&#125;&gt;</span><br><span class="line">                &lt;div className=<span class="string">"display"</span>&gt;</span><br><span class="line">                    &lt;input</span><br><span class="line">                        className=<span class="string">"check"</span></span><br><span class="line">                        type=<span class="string">"checkbox"</span></span><br><span class="line">                        checked=&#123;done&#125;</span><br><span class="line">                        onChange=&#123;(&#123;target&#125;) =&gt; &#123;</span><br><span class="line">                            dispatch(&#123;</span><br><span class="line">                                type: <span class="string">"DONE"</span>,</span><br><span class="line">                                id,</span><br><span class="line">                                done:target.checked,</span><br><span class="line">                            &#125;);</span><br><span class="line">                        &#125;&#125;</span><br><span class="line">                    /&gt;</span><br><span class="line">                    &lt;div</span><br><span class="line">                        className=<span class="string">"todo-content"</span></span><br><span class="line">                        onDoubleClick=&#123;() =&gt; &#123;</span><br><span class="line">                            setEdit(<span class="literal">true</span>);</span><br><span class="line">                        &#125;&#125;</span><br><span class="line">                    &gt;&#123;txt&#125;</span><br><span class="line">                    &lt;<span class="regexp">/div&gt;</span></span><br><span class="line"><span class="regexp">                    &lt;span</span></span><br><span class="line"><span class="regexp">                        className="todo-destroy"</span></span><br><span class="line"><span class="regexp">                        onClick=&#123;() =&gt; &#123;</span></span><br><span class="line"><span class="regexp">                            dispatch(&#123;</span></span><br><span class="line"><span class="regexp">                                type:"REMOVE",</span></span><br><span class="line"><span class="regexp">                                id</span></span><br><span class="line"><span class="regexp">                            &#125;);</span></span><br><span class="line"><span class="regexp">                        &#125;&#125;&gt;</span></span><br><span class="line"><span class="regexp">                    &lt;/</span>span&gt;</span><br><span class="line">                &lt;<span class="regexp">/div&gt;</span></span><br><span class="line"><span class="regexp">                &lt;div className="edit"&gt;</span></span><br><span class="line"><span class="regexp">                    &lt;input</span></span><br><span class="line"><span class="regexp">                        className="todo-input"</span></span><br><span class="line"><span class="regexp">                        type="text"</span></span><br><span class="line"><span class="regexp">                        value=&#123;inputVal&#125;</span></span><br><span class="line"><span class="regexp">                        ref=&#123;input&#125;</span></span><br><span class="line"><span class="regexp">                        onChange=&#123;(&#123;target&#125;) =&gt; &#123;</span></span><br><span class="line"><span class="regexp">                            setInputVal(target.value);</span></span><br><span class="line"><span class="regexp">                        &#125;&#125;</span></span><br><span class="line"><span class="regexp">                        onBlur = &#123;() =&gt; &#123;</span></span><br><span class="line"><span class="regexp">                            setEdit(false);</span></span><br><span class="line"><span class="regexp">                            if (inputVal.trim()) &#123;</span></span><br><span class="line"><span class="regexp">                                /</span><span class="regexp">/当输入为空时，则还是将之前保存的txt存进</span></span><br><span class="line"><span class="regexp">                                dispatch(&#123;</span></span><br><span class="line"><span class="regexp">                                    type:"EDIT",</span></span><br><span class="line"><span class="regexp">                                    id,</span></span><br><span class="line"><span class="regexp">                                    txt: inputVal,</span></span><br><span class="line"><span class="regexp">                                &#125;);</span></span><br><span class="line"><span class="regexp">                            &#125; else &#123;</span></span><br><span class="line"><span class="regexp">                                setInputVal(txt);</span></span><br><span class="line"><span class="regexp">                            &#125;</span></span><br><span class="line"><span class="regexp">                        &#125;&#125;</span></span><br><span class="line"><span class="regexp">                        /</span><span class="regexp">/失去焦点时的事件触发</span></span><br><span class="line"><span class="regexp">                    /</span>&gt;</span><br><span class="line">                &lt;<span class="regexp">/div&gt;</span></span><br><span class="line"><span class="regexp">            &lt;/</span>div&gt;</span><br><span class="line">        &lt;<span class="regexp">/li&gt;</span></span><br><span class="line"><span class="regexp">    )</span></span><br><span class="line"><span class="regexp">&#125;</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">export default Li;</span></span><br></pre></td></tr></table></figure>

<h2 id="state-js"><a href="#state-js" class="headerlink" title="state.js"></a>state.js</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">'react'</span>;</span><br><span class="line"><span class="keyword">import</span> &#123;connect&#125; <span class="keyword">from</span> <span class="string">"react-redux"</span>;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">State</span> (<span class="params">props</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">const</span> &#123;unDoneLength, doneLength, dispatch&#125; = props;</span><br><span class="line">    <span class="keyword">return</span> (<span class="xml"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"todo-stats"</span>&gt;</span></span></span><br><span class="line">        &lt;span className="todo-count"&gt;</span><br><span class="line">            &lt;span className="number"&gt;&#123;unDoneLength&#125;&lt;/span&gt; &lt;span className="word"&gt;项待完成&lt;/span&gt;</span><br><span class="line"><span class="xml">        <span class="tag">&lt;/<span class="name">span</span>&gt;</span></span></span><br><span class="line"><span class="xml">        &#123;</span></span><br><span class="line">            doneLength&gt;0?(&lt;span className="todo-clear"&gt;</span><br><span class="line">                &lt;a</span><br><span class="line">                    href="#"</span><br><span class="line">                    onClick=&#123;() =&gt; &#123;</span><br><span class="line">                        dispatch(&#123;</span><br><span class="line">                            type: "REMOVE_DONE"</span><br><span class="line">                        &#125;);</span><br><span class="line">                    &#125;&#125;&gt;</span><br><span class="line">                    Clear &lt;span&gt;&#123;doneLength&#125;&lt;/span&gt;已完成事项</span><br><span class="line">                &lt;/a&gt;</span><br><span class="line"><span class="xml">            <span class="tag">&lt;/<span class="name">span</span>&gt;</span>):""</span></span><br><span class="line"><span class="xml">        &#125;</span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> connect(<span class="function"><span class="params">state</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">let</span> unDoneLength = state.filter(<span class="function"><span class="params">item</span> =&gt;</span> !item.done).length;</span><br><span class="line">    <span class="keyword">let</span> doneLength =  state.length  - unDoneLength;</span><br><span class="line">    <span class="comment">//对state数据进行处理，从而确定传递给组件的props数据到底是什么样的，</span></span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">        unDoneLength,</span><br><span class="line">        doneLength</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)(State);</span><br></pre></td></tr></table></figure>


    </div>
    
    
    
    
    <div>
      
        <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
    
</div>

      
    </div>
        <div class="reward-container">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作！</div>
  <button onclick="var qr = document.getElementById('qr'); qr.style.display = (qr.style.display === 'none') ? 'block' : 'none';">
    打赏
  </button>
  <div id="qr" style="display: none;">
      
      <div style="display: inline-block;">
        <img src="/images/alipay.png" alt="Li Yudong 支付宝">
        <p>支付宝</p>
      </div>

  </div>
</div>

        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>Li Yudong
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="http://example.com/2021/05/14/react/react%20hooks/" title="react hooks初学">http://example.com/2021/05/14/react/react hooks/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>

    
      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/react%E5%88%9D%E5%AD%A6/" rel="tag"># react初学</a>
          </div>
    
        

    
        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2021/05/10/react/react%E5%88%9D%E5%AD%A6/" rel="prev" title="react初学">
      <i class="fa fa-chevron-left"></i> react初学
    </a></div>
      <div class="post-nav-item">
    <a href="/2021/05/19/node/koa%E6%BA%90%E7%A0%81/" rel="next" title="KOA源码">
      KOA源码 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#前言"><span class="nav-number">1.</span> <span class="nav-text">前言</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#todoList"><span class="nav-number">2.</span> <span class="nav-text">todoList</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#reducer"><span class="nav-number">2.1.</span> <span class="nav-text">reducer</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#App-js"><span class="nav-number">2.2.</span> <span class="nav-text">App.js</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Create-js"><span class="nav-number">2.3.</span> <span class="nav-text">Create.js</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Todos-js"><span class="nav-number">2.4.</span> <span class="nav-text">Todos.js</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#li-js"><span class="nav-number">2.5.</span> <span class="nav-text">li.js</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#state-js"><span class="nav-number">2.6.</span> <span class="nav-text">state.js</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Li Yudong"
      src="/images/author.jpg">
  <p class="site-author-name" itemprop="name">Li Yudong</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">95</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">14</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">18</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/tiarmor1" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;tiarmor1" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:1157019137@qq.com" title="E-Mail → mailto:1157019137@qq.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>
  <div class="cc-license motion-element" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" class="cc-opacity" rel="noopener" target="_blank"><img src="/images/cc-by-nc-sa.svg" alt="Creative Commons"></a>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 2020 – 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Li Yudong</span>
</div>

        








      </div>
    </footer>
  </div>

  
  
  <script color='0,0,0' opacity='0.5' zIndex='-1' count='150' src="/lib1/canvas-nest/canvas-nest.min.js"></script>
  <script src="/lib1/anime.min.js"></script>
  <script src="/lib1/velocity/velocity.min.js"></script>
  <script src="/lib1/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>













  

  

  
   <canvas class="fireworks" style="position: fixed;left: 0;top: 0;z-index: 1; pointer-events: none;" ></canvas> 
   <script type="text/javascript" src="//cdn.bootcss.com/animejs/2.2.0/anime.min.js"></script> 
   <script type="text/javascript" src="/js/fireworks.js"></script>
  
</body>
</html>

